package jjn.round2;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @author Jiang Jining
 * @since 2023-05-12 23:26
 */
public class Offer40_GetLeastNumbers {
    public int[] getLeastNumbers(int[] arr, int k) {
        Comparator<Integer> comparator = Integer::compare;
        Queue<Integer> queue = new PriorityQueue<>(comparator);
        for (int num : arr) {
            queue.offer(num);
        }
        int[] result = new int[k];
        for (int i = 0; i < k && !queue.isEmpty(); i++) {
            result[i] = queue.poll();
        }
        return result;
    }
}
